package com.cbj.db_work.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cbj.db_work.common.result.R;
import com.cbj.db_work.common.result.ResultCode;
import com.cbj.db_work.entity.Player;
import com.cbj.db_work.entity.query.PlayerQuery;
import com.cbj.db_work.entity.vo.PlayerVo;
import com.cbj.db_work.service.PlayerService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cbj
 * @since 2021-06-30
 */
@RestController
@RequestMapping("/db_work/player")
@CrossOrigin
public class PlayerController {
    @Autowired
    private PlayerService playerService;
    @PostMapping("/addPlayer")
    public R addPlayer(@RequestBody PlayerVo vo){
        boolean flag = playerService.addPlayer(vo);
        if(flag)
            return R.ok();
        return R.error();
    }
    @DeleteMapping("/deletePlayer/{playerId}")
    public R deletePlayer(@PathVariable String playerId){
        boolean flag = playerService.removePlayer(playerId);
        if(flag)
            return R.ok();
        return R.error();
    }
    @PutMapping("/updatePlayer")
    public R updatePlayer(@RequestBody PlayerVo vo){
        boolean flag = playerService.updatePlayer(vo);
        if(flag)
            return R.ok();
        return R.error();
    }
    @PostMapping("/pageCondition/{current}/{limit}")
    public R pageCondition(@PathVariable long current,
                                @PathVariable long limit,
                                @RequestBody(required = false) PlayerQuery query){
        Page<Player> page = new Page<>(current, limit);
        QueryWrapper wrapper = new QueryWrapper();
        String id = query.getId();
        String name = query.getName();
        String begin = query.getBegin();
        String end = query.getEnd();
        String nation = query.getNation();
        String position = query.getPosition();
        if(!StringUtils.isEmpty(id)){
            wrapper.eq("id",id);
        }
        if(!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(begin)){
            wrapper.ge("create_date",begin);
        }
        if (!StringUtils.isEmpty(end)){
            wrapper.le("modified_date",end);
        }
        if(!StringUtils.isEmpty(nation)){
            wrapper.like("nation",nation);
        }
        if(!StringUtils.isEmpty(position)){
            wrapper.like("position",position);
        }
        playerService.page(page, wrapper);
        long total = page.getTotal();
        List<Player> records = page.getRecords();
        if(records == null){
            return R.error();
        }
        List<Player> voList = new ArrayList<>();
        for (Player record : records) {
            voList.add(record);
        }
        return R.ok().data("total", total).data("rows", voList);
    }
    @GetMapping("/getAllPlayer")
    public R getAllPlayer(){
        List<Player> list = playerService.list(null);
        return R.ok().data("items",list);
    }
    @GetMapping("getPlayer/{id}")
    public R getPlayer(@PathVariable String id) {
        Player player = playerService.getById(id);
        return R.ok().data("player",player);
    }
}

